####
## Project:            Do Corporate Regulations Deter or Stimulate Investment? The Effect of the OECD Anti-Bribery Convention on FDI
## File name:          01-enforcement.R
## Description:        Loads data on anti-bribery law enforcement and produces Figure 2 for the paper. 
##                      It also computes median year of first-enforcement for other OECD Convention ratifiers
## Data sources:       Violation Tracker by Good Jobs First; Kaczmarek and Newman (2011, IO) replication data
## Programmed by:      Lorenzo Crippa
####

# Setup ----
rm(list = ls())
setwd("") # INSERT THE REPLICATION FOLDER FILE PATH HERE 

library(haven)
library(cowplot)
library(tidyverse)
library(showtext)
library(scales)

font_add("Times New Roman", "times new roman.ttf")
showtext_auto()

# set default theme:
theme <- theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
               panel.background = element_rect(fill = "transparent", color = NA),
               plot.background = element_rect(fill = "transparent", color = NA),
               axis.line = element_line(colour = "black", linewidth = 0.3),
               text = element_text(colour = "black", size = 14, family = "Times New Roman"))
theme_set(theme)

# Read data ----
## Good jobs first ----
gjf <- read_rds("data/gjf_FCPA.rds")

## Kaczmarek and Newman 2011 ----
kn <- read_dta("data/kaczmarek_newman2011.dta")

# when did OECD Convention ratifiers enforced their first case? (excluding US)
kn %>%
  filter(dv_case == 1) %>%
  select(country, year, dv_case) %>%
  select(year) %>% pull() %>%
  summary()

# Plots ----
## Figure 2 ----
# produce the panels and export them for plotting
fig2_a <- gjf %>%
  filter(year <= 2020) %>%
  group_by(year, HQ) %>%
  summarise(cases = n()) %>% 
  filter(is.na(year) != T) %>% 
  mutate(nat = factor(HQ, levels = c("US", "Non-US"), labels = c("US firms", "Non-US firms"))) %>%
  ggplot(aes(x = year, y = cases)) + 
  geom_line(aes(group = nat, linetype = nat)) +
  geom_rect(aes(ymin = 0, ymax = Inf, xmin = 2006, xmax = 2011), alpha = .005) +
  geom_text(data = data.frame(x = 2003, y = 15, label = "Firm-level\nanalysis"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, family = "Times New Roman") +
  geom_segment(data = data.frame(x = 2004+.1, xend = 2006-0.2,
                                 y = 15, yend = 15),
               aes(x = x, y = y, xend = xend, yend = yend),
               arrow = arrow(length = unit(0.2, "cm")),
               linewidth = .2, col = I("black")) +
  scale_x_continuous(breaks = c(1995, 2000, 2005, 2010, 2015, 2020)) + 
  scale_linetype_manual("", values = c("solid", "dashed")) + 
  ylab("Number of cases") +
  theme(legend.position = "none") + 
  guides(linetype = guide_legend(title.position="top", title.hjust = 0.5))
fig2_b <- gjf %>%
  filter(year <= 2020) %>%
  group_by(year, HQ) %>%
  summarise(fines = sum(penalty, na.rm = T)) %>% 
  filter(is.na(year) != T) %>% 
  mutate(nat = factor(HQ, levels = c("US", "Non-US"), labels = c("US firms", "Non-US firms")),
         l.fines = log(fines)) %>%
  # ggplot(aes(x = year, y = (fines/10^9), linetype = nat)) +
  ggplot(aes(x = year, y = fines)) +
  geom_line(aes(group = nat, linetype = nat)) +
  geom_rect(aes(ymin = 0, ymax = Inf, xmin = 2006, xmax = 2011), alpha = .005) +
  geom_text(data = data.frame(x = 2003, y = 10^9, label = "Firm-level\nanalysis"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, family = "Times New Roman") +
  geom_segment(data = data.frame(x = 2004+.1, xend = 2006-0.2,
                                 y = 10^9, yend = 10^9),
               aes(x = x, y = y, xend = xend, yend = yend),
               arrow = arrow(length = unit(0.2, "cm")),
               linewidth = .2, col = I("black")) +
  scale_x_continuous(breaks = c(1995, 2000, 2005, 2010, 2015, 2020)) +
  scale_y_continuous(trans = log10_trans(),
                     breaks = trans_breaks("log10", function(x) 10^x),
                     labels = trans_format("log10", math_format(10^.x))) +
  scale_linetype_manual("", values = c("solid", "dashed")) + 
  ylab("Fines (USD, logarithmic scale)") +
  theme(legend.position = "bottom", legend.box = 'horizontal',
        legend.key = element_blank(), legend.background = element_rect(fill = "transparent", color = NA)) + 
  guides(linetype = guide_legend(title.position="top", title.hjust = 0.5))

# produce and save the plot
plot_grid(fig2_a + labs(subtitle = "(a) Number of FCPA cases prosecuted by the US DOJ and SEC (2000-2020)") +
            xlab(""), 
          fig2_b + labs(subtitle = "(b) Corporate FCPA fines levied by the US DOJ and SEC (2000-2020)"), 
          align = "v", nrow = 2, rel_heights = c(0.35, 0.65))
dev.copy("plots/main_fig_2.pdf", width = 8.5, height = 5.8, device = pdf)
dev.off()

#====# The End #====#